package com.longjianghuasheng.logistics.business.mine.fragment

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import coil.load
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.MineViewModel
import com.longjianghuasheng.logistics.databinding.FragmentDriverMineBinding
import com.longjianghuasheng.logistics.ext.*
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding

/**
 * 司机端个人页面
 */
class DriverMineFragment : BaseFragment<MineViewModel, FragmentDriverMineBinding>() {

    override fun layoutId(): Int = R.layout.fragment_driver_mine

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.run {
            space.addStatusBarTopPadding()


            tvAll.onClickIsLogin { navigateOrder(0) }
            tvOrderReceived.onClickIsLogin { navigateOrder(1) }
            tvInProgress.onClickIsLogin { navigateOrder(2) }
            tvCompleted.onClickIsLogin { navigateOrder(3) }

            tvMineOrder.onClickIsLogin { navigate(R.id.orderFragment) }

            tvMineWallet.onClickIsLogin {
                navigate(
                    if (mViewModel.is_account == 1)
                        R.id.walletFragment
                    else
                        R.id.accountOpeningFragment
                )
            }

            tvMineBillingPeriod.onClickIsLogin {


                navigate(R.id.accountOpeningFragment, Bundle().apply {
                    putBoolean("isAccount", mViewModel.is_account == 1)
                })

            }

            tvAddressBook.onClickIsLogin { navigate(R.id.addressListFragment) }

            tvReleaseRecord.onClickIsLogin { navigate(R.id.releaseRecordFragment) }
            tvFleetRecruitment.onClickIsLogin { navigate(R.id.fleetRecruitmentFragment) }

            tvDriverRecruitment.onClickIsLogin { navigate(R.id.driverRecruitmentFragment) }

            tvFeedback.onClickIsLogin { navigate(R.id.feedbackFragment) }

            tvCommonProblem.onClickIsLogin { navigate(R.id.commonProblemFragment) }

            tvPrivacy.onClickIsLogin { navigate(R.id.privacyFragment) }

            tvAbout.onClickIsLogin {
                navigate(R.id.aboutFragment, Bundle().apply {
                    putInt("id", 8)
                })
            }

            tvSystemSettings.onClickIsLogin { navigate(R.id.systemSettingsFragment) }
            tvPersonalInformation.onClickIsLogin {
                if (!mViewModel.isJump) {
                    return@onClickIsLogin
                }

                navigate(R.id.userInfoFragment)
            }

            ivPhoto.onClickIsLogin {


                selectPicture {


                    mViewModel.avatar = it.realPath
                    ivPhoto.load(it.path)

                    mViewModel.avatar()
                }


            }

            tvContactCustomerService.onClickIsLogin {

                mViewModel.hot_line()
            }
        }
    }

    private fun navigateOrder(position: Int) {
        navigate(R.id.driverOutOrderFragment, Bundle().apply {
            putInt("position", position)
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            if (!it.avatar.isNullOrBlank()) {
                mDatabind.ivPhoto.loadUrl(it.avatar)
            }
            mDatabind.tvName.text = it.real_name
            mViewModel.is_account = it.is_account ?: 0


            mDatabind.tvPersonalInformation.text = when {
                it.is_company_work == 0 && it.is_people_work == 0 -> {
                    mViewModel.isJump = true
                    "完善实名认证信息"
                }
                it.is_company_work == 2 -> "企业"
                it.is_people_work == 2 -> "个人"
                it.is_company_work == 1 || it.is_people_work == 1 -> "认证中"
                else -> " "
            }


        }
        mViewModel.resultHot_line.observer(this) {
            openPhone(it)

        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.my_info()
    }

}